package com.zhangtai.modules.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.common.annotation.SysAuthorityLog;
import com.zhangtai.modules.dao.NoticeRoleDao;
import com.zhangtai.modules.dao.NoticeUserRoleDao;
import com.zhangtai.modules.entity.common.NoticeRoleEntity;
import com.zhangtai.modules.entity.common.NoticeUserRoleEntity;
import com.zhangtai.modules.service.common.NoticeRoleService;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("noticeRole")
@Api(tags="推送角色")
public class NoticeRoleController {
    @Autowired
    private NoticeRoleDao noticeRoleDao;
    @Autowired
    private NoticeRoleService noticeRoleService;
    @Autowired
    private NoticeUserRoleDao noticeUserRoleDao;

    @PostMapping("/getNoticeRoleList")
    @ApiOperation(value="获取通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R getNoticeRoleList( String name,@RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
       PageResult result =  noticeRoleService.queryPage(name,pageNum,pageSize);
       return  R.ok(result);
    }

    @GetMapping("/getNoticeRoleListNoPage")
    @ApiOperation(value="获取通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R getNoticeRoleList(){
        List<NoticeRoleEntity> roleList = noticeRoleDao.selectList(new QueryWrapper<NoticeRoleEntity>());
        return R.ok(roleList);
    }

    @GetMapping("/getNoticeRoleByUser")
    @ApiOperation(value="获取用户通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:get')")
    public R getNoticeRoleByUser(@RequestParam(value = "userId") Long userId){
        List<NoticeUserRoleEntity> roleList = noticeUserRoleDao.selectList(new QueryWrapper<NoticeUserRoleEntity>().eq("user_id",userId));
        return R.ok(roleList);
    }

    @PostMapping("/updateNoticeRoleList")
    @ApiOperation(value="更新/新增通知推送角色")
    @SysAuthorityLog(value = "更新/新增通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:get')")
    @Transactional
    public R updateNoticeRoleList(@RequestBody @Valid NoticeRoleEntity role){
        NoticeRoleEntity r = BeanUtilsMabach.doToDto(role,NoticeRoleEntity.class);
        if(ObjectUtils.isEmpty(r.getNoticeRoleId())){
            r.setCreateTime(new Date());
            noticeRoleDao.insert(r);
//            noticeRoleService.saveE(r);
        }else{
            noticeRoleService.updateByIdE(r);
        }
        return R.ok();
    }

    @DeleteMapping("/deleteNoticeRole")
    @ApiOperation(value="删除通知推送角色")
    @SysAuthorityLog(value = "删除通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:update')")
    @Transactional
    public R deleteNoticeRole(Long roleId){
        noticeRoleDao.delete(new QueryWrapper<NoticeRoleEntity>().eq("notice_role_id",roleId));
        noticeUserRoleDao.delete(new QueryWrapper<NoticeUserRoleEntity>().eq("notice_role_id",roleId));
        return R.ok();
    }

    @PostMapping("/saveOrUpdateNoticeUserRole")
    @ApiOperation(value="新增/更新用户通知推送角色")
    @SysAuthorityLog(value = "新增/更新用户通知推送角色")
    @PreAuthorize("hasAuthority('sys:admin:update')")
    @Transactional
    public R saveOrUpdateNoticeUserRole(String[] roleId,Long userId){
        noticeUserRoleDao.delete(new QueryWrapper<NoticeUserRoleEntity>().eq("user_id",userId));
        if(!ObjectUtils.isEmpty(roleId)){
            for(String role:roleId){
                NoticeUserRoleEntity roleEntity = new NoticeUserRoleEntity();
                roleEntity.setUserId(userId);
                roleEntity.setNoticeRoleId(Long.parseLong(role));
                noticeUserRoleDao.insert(roleEntity);
            }
        }
        return R.ok();
    }


}
